<?php

//cu acest fisier postez in baza de date

include_once "funct.php";

if (!isset($_POST["id"], $_POST["content"], $_SESSION["user"]))
	iesire();
clean($_POST["id"]);
clean($_POST["content"]);
$room = room($_POST["id"]);
if ($room === false || ($room["user"] != $_SESSION["user"] && $room["user2"] != $_SESSION["user"])) {
	back();
	iesire(); //daca nu e gasita camera sau s-a terminat batalia
}
if ($room["terminat"] == 0 && $room["user2"] != -1) {
	$rezultat = mysql_query("select * from " . $db["messages"] . " where room='" . $room["id"] . "' and	user='" . $_SESSION["user"] . "'");
	if (mysql_num_rows($rezultat) < 3) {
		$rezultat = mysql_query("select * from " . $db["messages"] . " where room='" . $room["id"] . "' order by id desc");
		$rand = mysql_fetch_array($rezultat);
		if (count($rand) == 0 && $room["user2"] == $_SESSION["user"])
			$ok = 1;
		else if ($rand["user"] != $_SESSION["user"])
			$ok = 1;
		else
			$ok = 0;
		if ($ok == 1)
			mysql_query("insert into messages set room='" . $_POST["id"] . "', user='" . $_SESSION["user"] . "', content='" . $_POST["content"] . "', date='" . ceas() . "'");
	} else {
		$rezultat = mysql_query("select * from " . $db["messages"] . " where room='" . $room["id"] . "'");
		if (mysql_num_rows($rezultat) >= 6) {
			out_room($room["id"]);
		}
	}
}
?>
